package bsc.war.handler;

import bsc.ejb.entity.Dimension;
import bsc.ejb.entity.DimensionMember;
import bsc.ejb.entity.DimensionalCut;
import bsc.ejb.entity.Person;
import bsc.ejb.entity.Pointer;
import bsc.ejb.entity.Project;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.entity.ScoreDomain;
import bsc.ejb.metadata.Colorimetry;
import bsc.ejb.metadata.Comparation;
import bsc.ejb.metadata.Framework;
import bsc.ejb.metadata.MapDrawing;
import bsc.ejb.metadata.Unit;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.ScoreCardUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.ListUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author memozac
 */
public class MainDesigner extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            List<ScoreDomain> domains = delegate.findScoreDomainSpecial(1L);
            List<Person> persons = delegate.findPerson();
            List<ScoreCard> scoreCards = delegate.findScoreCard();

            List<Pointer> pointers = new ArrayList<Pointer>();
            List<DimensionalCut> dimensionalCuts = new ArrayList<DimensionalCut>();

            List<Project> projects = new ArrayList<Project>();
            List<bsc.ejb.entity.Map> maps = new ArrayList<bsc.ejb.entity.Map>();
            List<Dimension> dimensions = delegate.findDimension();



            for (ScoreCard scoreCard : scoreCards) {
                
                if (scoreCard instanceof Pointer) {
                    pointers.add((Pointer) scoreCard);
                }
                if (scoreCard instanceof DimensionalCut) {
                    dimensionalCuts.add((DimensionalCut) scoreCard);
                }
                if (scoreCard instanceof Project) {
                    projects.add((Project) scoreCard);
                }
                if (scoreCard instanceof bsc.ejb.entity.Map) {
                    maps.add((bsc.ejb.entity.Map) scoreCard);
                }
            }

            //Se limpia la lista de scorecards
            scoreCards = ListUtils.subtract(scoreCards, pointers);
            scoreCards = ListUtils.subtract(scoreCards, projects);
            scoreCards = ListUtils.subtract(scoreCards, maps);

            //Se limpia la lista de pointers
            pointers = ListUtils.subtract(pointers, dimensionalCuts);

            //Se buscan los miembros de cada de lo Cut-Pointers
            Map<DimensionalCut, String> dimensionalCutsMembers = ScoreCardUtils.getDimensionalCutsMembersStrings(request, delegate, dimensionalCuts);


            Map<Dimension, List<DimensionMember>> dimensionMembers = new HashMap<Dimension, List<DimensionMember>>();
            for (Dimension dimension : dimensions) {
                List<DimensionMember> dimensionMember = delegate.findDimensionMember(dimension);
                dimensionMembers.put(dimension, dimensionMember);
            }

            //remove system users
            persons.remove(delegate.findPerson(1L));
            persons.remove(delegate.findPerson(2L));

            //Find the Active Framework
            Framework framework= delegate.findFramework(Boolean.TRUE);

            //Upload colorimetries
            List<Colorimetry> colorimetries= delegate.findColorimetry(framework, Boolean.TRUE);

            //Upload units
            List<Unit> units = delegate.findUnit(framework, Boolean.TRUE);

            //Upload comparations (Type of Comparators)
            List<Comparation> comparations = delegate.findComparation(framework, Boolean.TRUE);
            
            //Upload MapDrawings
            List<MapDrawing> mapDrawings = delegate.findMapDrawing(framework, Boolean.TRUE);

            request.setAttribute("colorimetries", colorimetries);
            request.setAttribute("units", units);
            request.setAttribute("comparations", comparations);
            request.setAttribute("mapDrawings", mapDrawings);

            request.setAttribute("domains", domains);
            request.setAttribute("persons", persons);
            request.setAttribute("scoreCards", scoreCards);
            request.setAttribute("maps", maps);
            request.setAttribute("pointers", pointers);
            request.setAttribute("dimensionalCuts", dimensionalCuts);
            request.setAttribute("dimensionalCutsMembers", dimensionalCutsMembers);
            request.setAttribute("projects", projects);
            request.setAttribute("dimensions", dimensions);
            request.setAttribute("dimensionMembers", dimensionMembers);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ModelAndView("mainDesigner");
    }
}
